package com.dlc.felear.xiaoensale.activity;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.adapter.ActionItemAdapter;
import com.dlc.felear.xiaoensale.adapter.ActionItemStepAdapter;
import com.dlc.felear.xiaoensale.constant.Constant;
import com.dlc.felear.xiaoensale.dao.ActionStepItemDao;
import com.dlc.felear.xiaoensale.deviceUtil.TopbarHelper;
import com.dlc.felear.xiaoensale.entity.ActionStepItem;
import com.dlc.felear.xiaoensale.helper.JsonHelper;
import com.dlc.felear.xiaoensale.helper.SelectActionHelper;
import com.itdlc.android.library.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * 操作步骤选择页面
 */
public class SelectperatorActivity extends BaseActivity {
    private static final String TAG = "SelectperatorActivity";
    private GridView gridView1;
    private GridView gridView2;
    private GridView gridView3;
    private ActionItemAdapter actionItemAdapter;
    private ActionItemStepAdapter actionItemStepAdapter1;
    private ActionItemStepAdapter actionItemStepAdapter2;
    private TopbarHelper topbarHelper;
    private ActionStepItemDao actionStepItemDao;
    private SelectActionHelper selectActionHelper;


    @Override
    protected void initialView() {
        gridView1 = findViewById(R.id.m_gv1);
        gridView2 = findViewById(R.id.m_gv2);
        gridView3 = findViewById(R.id.m_gv3);
        topbarHelper = new TopbarHelper(this);
        topbarHelper.setReturnEnable(true);
        topbarHelper.setNotReturn("操作不能为空，请选择一个操作");
        topbarHelper.setTitle("选择功能");
        actionStepItemDao = new ActionStepItemDao(this);
        selectActionHelper = new SelectActionHelper(this);
        initGridView();
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_manual_select;
    }

    private void initGridView() {
        actionItemAdapter = new ActionItemAdapter(this);
        gridView1.setAdapter(actionItemAdapter);
        actionItemAdapter.setItems(handleActionStepItem());
        gridView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                parserAction(1, actionItemAdapter.getItem(position), position);
            }
        });

        actionItemStepAdapter1 = new ActionItemStepAdapter(this);
        gridView2.setAdapter(actionItemStepAdapter1);
        actionItemStepAdapter1.setItems(actionStepItemDao.queryForAllByAction(Constant.ACTION_STEP_JGT));
        gridView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                parserAction(2, actionItemStepAdapter1.getItem(position), position);
            }
        });

        actionItemStepAdapter2 = new ActionItemStepAdapter(this);
        gridView3.setAdapter(actionItemStepAdapter2);
        actionItemStepAdapter2.setItems(actionStepItemDao.queryForAllByAction(Constant.ACTION_STEP_JYT));
        gridView3.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                parserAction(3, actionItemStepAdapter2.getItem(position), position);
            }
        });


    }

    private List<ActionStepItem> handleActionStepItem(){
        List<ActionStepItem> list=actionStepItemDao.queryForAllByAction(Constant.ACTION_STEP_GDZ);
        List<ActionStepItem> data=new ArrayList<>();
        for(ActionStepItem actionStepItem:list){
            boolean tempFlag=TextUtils.equals(actionStepItem.getName(),"复位")||
                    TextUtils.equals(actionStepItem.getName(),"出锅")||
                    TextUtils.equals(actionStepItem.getName(),"洗锅");
            if(!tempFlag){
                data.add(actionStepItem);
            }
        }
        Log.e(TAG,"actions:"+ JsonHelper.toJson(list));
        return data;
    }

    private void parserAction(int i, ActionStepItem item, int position) {
        try {
            selectActionHelper.parserAction(i, item);
        }catch (Exception e){
            Log.e(TAG,e.getMessage());
        }

    }


}
